home *** CD-ROM | disk | FTP | other *** search
- Path: ecmwf.int!munin!mab
- From: mab@ecmwf.co.uk (Baudouin Raoult)
- Newsgroups: comp.lang.c++
- Subject: locking
- Date: 10 Jan 1996 14:41:31 GMT
- Organization: European Centre for Medium Range Weather Forecasts
- Message-ID: <4d0j6r$1ri@daphne.ecmwf.int>
- NNTP-Posting-Host: munin.ecmwf.int
- X-Newsreader: TIN [version 1.2 PL2]
-
- Hello,
-
- I have an interesting problem. I would like to create an object
- in shared memory, and lock/unlock it when accessing it:
-
- class foo {
- public:
- void bar();
- };
-
- void main()
- {
- SharedObject<foo> fooH;
-
- fooH->bar();
- }
-
- When calling foo::bar, a lock must be set and reset. Using the
- operator-> does not help, because I cannot write
-
- SharedObject::operator->()
- {
- lock();
- return object;
- unlock();
- }
-
- because the unlock is never executed. I solved my problem by adding
- a temporary object:
-
- SharedObject::operator->()
- {
- return LockObject(object);
- }
-
- with
-
- LockObject::LockObject(o) : object(o)
- {
- lock();
- }
-
- LockObject::~LockObject()
- {
- unlock();
- }
-
- LockObject::operator->()
- {
- return object;
- }
-
- This seems to work, but the LockObject is only destroyed at the
- end of the block, locking my object for too long.
-
- main()
- {
-
- SharedObject<foo> fooH("lock");
-
- ... the lock is not set
-
- fooH->bar();
-
- .. the lock is set until the end.
- }
-
- Anyone got an idea ?
-
- Thanks,
- Baudouin
-
- --
-
- ---------------------------------------------------
- Baudouin Raoult.
- European Center for Medium Range Weather Forecast
- Reading, UK
- ---------------------------------------------------
-